//
//  HTTabBar.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/16.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
// 声明一个协议
protocol CZTabBarDelegate: NSObjectProtocol{
    // 协议方法
    func composeButtonClick()
}
class HTTabBar: UITabBar{
    
    // 声明一个代理 (声明一个代理需要用weak 修饰 方法循环引用  使用weak 修饰以后 必须 继承基协议 NSObjectProtocol)
    weak var czDeletate:CZTabBarDelegate?
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setUpUI()
    }
    
    
    //设置
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 监听方法
    @objc private func composeButtonClick(){
        czDeletate?.composeButtonClick()
    }

    //设置控件
    func setUpUI(){
        
        
        addSubview(composetButton)
        
    }
    //撰写按钮的懒加载
    private lazy var composetButton : UIButton = {
        //创建按钮
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        //
        button.sizeToFit()
        
        //返回按钮
        return button
    }()
    
    //设置Frame /控件布局
   override func layoutSubviews(){
    
     super.layoutSubviews()
    
    
     //按钮的宽度
    let Width = SCREENW / 5
    
    //设置按钮的index
    var index : CGFloat = 0;
    
    //遍历子控件
    for values in self.subviews{
        //取出所有的UIBarButton
        if values.isKindOfClass( NSClassFromString("UITabBarButton")!){
            //设置X
            values.frame.origin.x = index * Width
            
            //设置W
            values.frame.size.width = Width
            //按钮索引
            index++
            //判断如果是2 再++
            if index == 2 {
                
                index++
            }
            
        }
        //设置撰写按钮x
        composetButton.center.x = self.frame.size.width * 0.5
        composetButton.center.y = self.frame.size.height * 0.5
    
    }

    }

}
